package com.zhixun.mobile.modules.jiujiudevice.device.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderLog;
import com.boxin.ims.modules.jiujiudevice.entity.JiuOrderRecord;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderLogService;
import com.boxin.ims.modules.jiujiudevice.service.JiuOrderRecordService;

/**
 * 
 * @author sxh
 * @date 2018年11月26日 上午11:19:06
 * @function <p> 自动删除订单金额为0的订单 </p>
 *
 */
@Service
public class AutoCloseZeroOrderThread implements Runnable{
	private JiuOrderRecordService jiuOrderRecordService;
	 
	@Autowired
	public AutoCloseZeroOrderThread(JiuOrderRecordService jiuOrderRecordService) {
		// TODO Auto-generated constructor stub
		this.jiuOrderRecordService = jiuOrderRecordService;
		if(null != jiuOrderRecordService){
			new Thread(this).start();
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("自动清除设备结束使用且金额为零的订单和订单日志线程已经启动");
		while(true){
			System.out.println("自动清除设备结束使用且金额为零的订单和订单日志线程又开始启动了");
			List<JiuOrderRecord> jiuOrderRecordList = jiuOrderRecordService.findAll();
			if(jiuOrderRecordList != null){
				for (int i = 0; i < jiuOrderRecordList.size(); i++) {
					JiuOrderRecord jiu = jiuOrderRecordList.get(i);
					//如果金额为0的话，删除
					if(jiu != null ){
						
					    if(jiu.getAmount() != null && jiu.getAmount() == 0 && jiu.getEndTime() != null ){
					    	jiu.setDelFlag(JiuOrderRecord.DEL_YES);
					    	jiuOrderRecordService.save(jiu);
					    }
						
					}
                           
				}
			  }
			try {
				Thread.sleep(1000*3);
			} catch (InterruptedException e) {
				System.err.println("清除过期订单的线程被中断");
				e.printStackTrace();
			}
			
		}
	}
}
